﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Configuration;
using Lephone.Data;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Threading;

namespace SimlvClient.Pages
{
    /// <summary>
    /// Interaction logic for FTP.xaml
    /// </summary>
    public partial class FTP : UserControl
    {
        public FTP()
        {
            InitializeComponent();

            string ftpServerIP = ConfigurationManager.AppSettings["ftpServerIP"];
            string ftpUserID = ConfigurationManager.AppSettings["ftpUserID"];
            string ftpPassword = ConfigurationManager.AppSettings["ftpPassword"];
            this.FtpAddress.Text = ftpServerIP;
            this.FtpUser.Text = ftpUserID;
            //Count
            var timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(20000000);   //时间间隔为一秒
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
           this.Dispatcher.Invoke(new Action(() =>
            {
                var Lists = DbEntry.From<SimlvClient.Model.OperPageHistory>().Where(Lephone.Data.Condition.Empty).Select();
                this.Count.Text = Lists.Count().ToString();
            }));
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Thread thread = new Thread(new ThreadStart(AllPage));
            thread.Start();
        }

        public void AllPage()
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
                this.IsBusy.Visibility = Visibility.Visible;
                this.IsBusy.IsIndeterminate = true;
                BtnAll.IsEnabled = false;
                BtnPart.IsEnabled = false;
            }));
            var List = DbEntry.From<SimlvClient.Model.News>().Where(Lephone.Data.Condition.Empty).Select();
            foreach (var Models in List)
            {
                SimlvClient.Model.OperPageHistory ModelHistory = new Model.OperPageHistory()
                {
                    CateId = Models.CateId,
                    DataId = Models.Id,
                    IsSimple = Models.IsSimple,
                    OperName = "Edit",
                };
                ModelHistory.Save();
            }

            string ftpServerIP = ConfigurationManager.AppSettings["ftpServerIP"]; 
            string ftpUserID = ConfigurationManager.AppSettings["ftpUserID"]; 
            string ftpPassword = ConfigurationManager.AppSettings["ftpPassword"]; 

            FTPHelper fhelper = new FTPHelper(ftpServerIP, ftpUserID, ftpPassword);
            fhelper.UpdatePage();
            this.Dispatcher.Invoke(new Action(() =>
            {
                this.IsBusy.Visibility = Visibility.Collapsed;
                this.IsBusy.IsIndeterminate = false;
                BtnAll.IsEnabled = true;
                BtnPart.IsEnabled = true;
            }));
        }

        /// <summary>
        /// a part update page
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            Thread thread = new Thread(new ThreadStart(PartUpdate));
            thread.Start();
            
        }

        public void  PartUpdate()
        {
            Dispatcher.Invoke(new Action(() =>
            {
                IsBusy.Visibility = Visibility.Visible;
                IsBusy.IsIndeterminate = true;
                BtnAll.IsEnabled = false;
                BtnPart.IsEnabled = false;
            }));
            string ftpServerIP = ConfigurationManager.AppSettings["ftpServerIP"];
            string ftpUserID = ConfigurationManager.AppSettings["ftpUserID"];
            string ftpPassword = ConfigurationManager.AppSettings["ftpPassword"];

            FTPHelper fhelper = new FTPHelper(ftpServerIP, ftpUserID, ftpPassword);
            fhelper.UpdatePage();
            Dispatcher.Invoke(new Action(() =>
            {
                IsBusy.Visibility = Visibility.Collapsed;
                IsBusy.IsIndeterminate = false;
                BtnAll.IsEnabled = true;
                BtnPart.IsEnabled = true;
            }));
        }
    }
}
